/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.groupjjk.spectatorsportsonline;

import com.groupjjk.spectatorsportsonline.models.NBA.ModelQuarterType;
import com.groupjjk.spectatorsportsonline.models.NBA.QuarterType;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jrdevelopment
 */
public class ServletController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
         //   insert(new QuarterType());
            //campos hidden de los formularios para manejar el controlador
            String controller = request.getParameter("controller");
            String action = request.getParameter("action");
            String mensaje = "";

//            No podemos hacer un swich porque en esta version aun no soporta un string
            //Crud quarterType
            if ("quarterType".equalsIgnoreCase(controller)) {
                // insodelQuarterType modelQuarterType = new ModelQuarterType();tanciamos el Bean y El modelo
                QuarterType quarterType = new QuarterType();
                ModelQuarterType modelQuarterType = new ModelQuarterType();
                ArrayList<QuarterType> LquarterTypes = new ArrayList();

                if (action.equalsIgnoreCase("create")) {
                    //seteamos el objeto
                    quarterType.setDescription(request.getParameter("descripcion"));

                    if (modelQuarterType.insert(quarterType)) {
                        request.setAttribute("styles", mensaje = "Create sucessful");
                        RequestDispatcher view = request.getRequestDispatcher("quarterTypeResponse.jsp");
                        view.forward(request, response);
                    } else {
                        response.sendError(503);
                    }

                }
                if (action.equalsIgnoreCase("update")) {
                    //seteamos el objeto
                    quarterType.setId(Long.valueOf(request.getParameter("id")));
                    quarterType.setDescription(request.getParameter("descripcion"));
                    if (modelQuarterType.update(quarterType)) {
                        request.setAttribute("styles", mensaje = "Update sucessful");
                        RequestDispatcher view = request.getRequestDispatcher("quarterTypeResponse.jsp");
                        view.forward(request, response);
                    } else {
                        response.sendError(503);
                    }
                }
                if (action.equalsIgnoreCase("read")) {
                    LquarterTypes = modelQuarterType.read();
                    request.setAttribute("list", LquarterTypes);

                    RequestDispatcher view = request.getRequestDispatcher("quarterTypeRead.jsp");
                    view.forward(request, response);
                } else {
                    response.sendError(503);
                }


                if (action.equalsIgnoreCase("delete")) {
                    if (modelQuarterType.delete(quarterType)) {
                        request.setAttribute("styles", mensaje = "Delete sucessful");
                        RequestDispatcher view = request.getRequestDispatcher("quarterTypeResponse.jsp");
                        view.forward(request, response);
                    } else {
                        response.sendError(503);
                    }
                }


            }










        } finally {
            out.close();
        }
    }

    public void insert(QuarterType q) {
        q.setDescription("something");
        String UNIDAD_DE_PERSISTENCIA = "spectator-sports-online";
        //EntityManagerFactory factory = Persistence.createEntityManagerFactory(UNIDAD_DE_PERSISTENCIA, System.getProperties());
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(UNIDAD_DE_PERSISTENCIA);
        EntityManager em = factory.createEntityManager();


        em.getTransaction().begin();
        em.persist((QuarterType) q);
        em.flush();
        em.getTransaction().commit();



    }
 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP
 * <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
        public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
